< Prev(J) / Next(K) > / View :

Filename: /text/sbasic/shared/01020100.xhp

(section: variable) (bookmark: bm_id3149346)
names of variables
variables; using
types of variables
declaring variables
values;of variables
constants
arrays;declaring
defining;constants

変数の名前
変数; 型と名前

Using Variables
変数の使用法(hd_id3149346.1)

The following describes the basic use of variables in Office Basic.
Office Basic の基本的な変数の使用法について説明します。(par_id3154346.3)

(/section: variable)

Naming Conventions for Variable Identifiers
変数の命名規則(hd_id3153361.4)

A variable name can consist of a maximum of 255 characters. The first character of a variable name must be a letter A-Z or a-z. Numbers can also be used in a variable name, but punctuation symbols and special characters are not permitted, with exception of the underscore character ("_"). In Office Basic variable identifiers are not case-sensitive. Variable names may contain spaces but must be enclosed in square brackets if they do.
変数名には、最大 255 文字を使用できます。変数名の最初の文字には、A-Z ないし a-z のいずれかを使用する必要があります。変数名には数字も使用できますが、アンダースコア (「_」) 以外の句読記号や特殊記号は使用できません。Office Basic の変数名では、大文字と小文字は区別されません。変数名にはスペース記号も使用できますが、その場合は大かっこで囲む必要があります。(par_id3148797.5)

Examples for variable identifiers:
下記は変数名の例です。(par_id3156422.6)

MyNumber=5
MyNumber=5(par_id3163798.7)

Correct
正しい変数名です。(par_id3156441.126)

MyNumber5=15
MyNumber5=15(par_id3147317.8)

Correct
正しい変数名です。(par_id3149664.127)

MyNumber_5=20
MyNumber_5=20(par_id3145364.9)

Correct
正しい変数名です。(par_id3146119.128)

My Number=20
My Number=20(par_id3154729.10)

Not valid, variable with space must be enclosed in square brackets
不正な変数名です。スペース記号を含む変数名は、大かっこで囲む必要があります。(par_id3153876.11)

[My Number]=12
[My Number]=12(par_id3147126.14)

Correct
正しい変数名です。(par_id3154510.15)

DéjàVu=25
DéjáVu=25(par_id3153708.12)

Not valid, special characters are not allowed
不正な変数名です。変数名に特殊文字は使用できません。(par_id3150330.129)

5MyNumber=12
5MyNumber=12(par_id3155443.13)

Not valid, variable may not begin with a number
不正な変数名です。変数名の先頭に数字は使えません。(par_id3154254.130)

Number,Mine=12
Number,Mine=12(par_id3147345.16)

Not valid, punctuation marks are not allowed
不正な変数名です。変数名に句読記号は使用できません。(par_id3149256.131)


Declaring Variables
変数の宣言(hd_id3146317.17)

In Office Basic you don't need to declare variables explicitly. A variable declaration can be performed with the Dim statement. You can declare more than one variable at a time by separating the names with a comma. To define the variable type, use either a type-declaration sign after the name, or the appropriate key word.
Office Basic では、必ずしも変数を明示的に宣言する必要はありません。変数の宣言は、Dim ステートメントで行います。変数名をコンマで区切ることで、1 度に複数の変数を宣言できます。変数型を指定するには、変数名に続けて、型宣言子ないしは該当するキーワードを付けます。(par_id3150299.18)

Examples for variable declarations:
下記は型宣言の例です。(par_id3154118.140)

DIM a$
DIM a$(par_id3150090.19)

Declares the variable "a" as a String
変数「a」を文字列として宣言します。(par_id3150982.132)

DIM a As String
DIM a As String(par_id3149531.20)

Declares the variable "a" as a String
変数「a」を文字列として宣言します。(par_id3150343.133)

DIM a$, b As Integer
DIM a$, b As Integer(par_id3149036.21)

Declares one variable as a String and one as an Integer
前者の変数を文字列、後者の変数を整数として宣言します。(par_id3155507.22)

DIM c As Boolean
DIM c As Boolean(par_idN10854.)

Declares c as a Boolean variable that can be TRUE or FALSE
TRUE または FALSE を取るブール型変数として c を宣言します(par_idN10859.)


It is very important when declaring variables that you use the type-declaration character each time, even if it was used in the declaration instead of a keyword. Thus the following statements are invalid:
型宣言を行う際には、キーワードではなく型宣言子を用いる場合でも、個々の変数名ごとに型宣言子を付ける必要があるので、この点には特に注意してください。たとえば、下記のような型宣言は無効です。(par_id3150519.23)

DIM a$
DIM a$(par_id3152985.24)

Declares "a" as a String
変数「a」を文字列として宣言します。(par_id3154527.134)

a="TestString"
a="TestString"(par_id3148599.25)

Type-declaration missing: "a$="
この場合は次のような型宣言が必要です。"a$="(par_id3153064.135)


Warning Icon 警告マーク Once you have declared a variable as a certain type, you cannot declare the variable under the same name again as a different type!
特定の変数型として宣言した変数名は、別の型で再宣言することはできません。(par_id3144770.26)

Forcing Variable Declarations
強制的な変数宣言(hd_id3149331.27)

To force declaration of variables, use the following command:
強制的に変数宣言をするには、下記のコマンドを使用します。(par_id3149443.28)

OPTION EXPLICIT
OPTION EXPLICIT(par_id3152869.29)

The Option Explicit statement has to be the first line in the module, before the first SUB. Generally, only arrays need to be declared explicitly. All other variables are declared according to the type-declaration character, or - if omitted - as the default type Single.
Option Explicit ステートメントは、モジュールの 1 行目に置き、最初の SUB の前に記述する必要があります。通常こうした明示的な宣言をする必要があるのは、配列だけです。その他の変数は、型宣言子を使って型宣言をすればよく、型宣言が省略された場合はデフォルトで 単精度型 とされます。(par_id3155072.30)

Variable Types
変数型(hd_id3154614.34)

Office Basic supports four variable classes:
Office Basic のサポートする変数には 4 つのクラスがあります。+(par_id3155383.35)

Integer Variables
整数変数(hd_id3153805.40)

Integer variables range from -32768 to 32767. If you assign a floating-point value to an integer variable, the decimal places are rounded to the next integer. Integer variables are rapidly calculated in procedures and are suitable for counter variables in loops. An integer variable only requires two bytes of memory. "%" is the type-declaration character.
整数変数には -32768 から 32767 までの整数を収めることができます。こうした整数変数に浮動小数点型の数値を代入すると、小数点以下を丸めた整数値が収められます。整数変数は、プロシージャー内で高速計算が可能であり、またループカウンタ用の変数としても適しています。整数変数が消費するメモリ量は、2 バイトです。型宣言用の記号は「%」です。(par_id3146966.41)

Dim Variable%
Dim 変数名%(par_id3153810.43)

Dim Variable As Integer
Dim 変数名 As Integer(par_id3153556.44)

Long Integer Variables
ロング整数変数(hd_id3147546.45)

Long integer variables range from -2147483648 to 2147483647. If you assign a floating-point value to a long integer variable, the decimal places are rounded to the next integer. Long integer variables are rapidly calculated in procedures and are suitable for counter variables in loops for large values. A long integer variable requires four bytes of memory. "&" is the type-declaration character.
ロング整数変数には -2147483648 から 2147483647 までの整数を収めることができます。このロング整数変数に浮動小数点型の数値を代入すると、小数点以下を丸めた整数値が収められます。ロング整数変数は、プロシージャー内で高速計算が可能であり、またループカウンタ用の変数としても適しています。ロング整数変数が消費するメモリ量は4バイトです。型宣言用の記号は「&」です。(par_id3151193.46)

Dim Variable&
Dim 変数名&(par_id3154708.48)

Dim Variable as Long
Dim 変数名 as Long(par_id3156365.49)

Decimal Variables
10 進数の変数(hd_id7596972.)

Decimal variables can take positive or negative numbers or zero. Accuracy is up to 29 digits.
10 進数の変数は、正の数、負の数、または 0 を受け取ることができます。 桁数は 29 桁までです。(par_id2649311.)

You can use plus (+) or minus (-) signs as prefixes for decimal numbers (with or without spaces).
10 進数には、プラス (+) またはマイナス (-) 記号をプレフィックスとして使用できます (スペースあり、またはスペースなし)。(par_id7617114.)

If a decimal number is assigned to an integer variable, Office Basic rounds the figure up or down.
整数変数に10 進数が代入される場合、Office Basic は数字を切り上げまたは切り捨てます。(par_id1593676.)

Single Variables
単精度変数(hd_id3147500.50)

Single variables can take positive or negative values ranging from 3.402823 x 10E38 to 1.401298 x 10E-45. Single variables are floating-point variables, in which the decimal precision decreases as the non-decimal part of the number increases. Single variables are suitable for mathematical calculations of average precision. Calculations require more time than for Integer variables, but are faster than calculations with Double variables. A Single variable requires 4 bytes of memory. The type-declaration character is "!".
単精度変数は 3.402823 × 10E38 から 1.401298 × 10E-45 までの正または負の値を取ります。単精度変数は浮動小数点方式で数値を扱うため、整数部の桁数が大きくなるほど小数部の精度が小さくなります。 単精度変数の用途としては、それほど高い精度を必要としない数学的な計算処理に適しています。 この場合の計算処理は、整数変数の場合よりも長くかかりますが、倍精度変数よりは短時間で済みます。 単精度変数が消費するメモリ量は 4 バイトです。 型宣言用の記号は「!」です。(par_id3153070.51)

Dim Variable!
Dim 変数名!(par_id3149875.52)

Dim Variable as Single
Dim 変数名 as Single(par_id3153302.53)

Double Variables
倍精度変数(hd_id3155753.54)

Double variables can take positive or negative values ranging from 1.79769313486232 x 10E308 to 4.94065645841247 x 10E-324. Double variables are floating-point variables, in which the decimal precision decreases as the non-decimal part of the number increases. Double variables are suitable for precise calculations. Calculations require more time than for Single variables. A Double variable requires 8 bytes of memory. The type-declaration character is "#".
倍精度変数には 1.79769313486232 × 10E308 から 4.94065645841247 × 10E-324 までの正および負の数値を収めることができます。倍精度変数は浮動小数点方式で数値を扱うため、整数部の桁数が大きくなるほど小数部の精度が小さくなります。 倍精度変数の用途としては、高い精度を必要とする計算処理に適しています。 この場合の計算処理は、単精度変数の場合よりも長くかかります。 倍精度変数が消費するメモリ量は 8 バイトです。 型宣言用の記号は「#」です。(par_id3150953.55)

Dim Variable#
Dim 変数名#(par_id3150431.56)

Dim Variable As Double
Dim 変数名 As Double(par_id3154406.57)

Currency Variables
通貨変数(hd_id3155747.95)

Currency variables are internally stored as 64-bit numbers (8 Bytes) and displayed as a fixed-decimal number with 15 non-decimal and 4 decimal places. The values range from -922337203685477.5808 to +922337203685477.5807. Currency variables are used to calculate currency values with a high precision. The type-declaration character is "@".
通貨変数は、内部的には 64 ビット (8 バイト) の数値として格納され、整数部 15 桁と小数部 4 桁の固定小数点方式として表示されます。 通貨変数は -922337203685477.5808 から +922337203685477.5807 までの値を取ります。通貨変数は、高い精度を必要とする金額計算に使用します。 型宣言用の記号は「@」です。(par_id3153337.96)

Dim Variable@
Dim 変数名@(par_id3147296.97)

Dim Variable As Currency
Dim 変数名 As Currency(par_id3150391.98)

String Variables
文字列変数(hd_id3148742.58)

String variables can hold character strings with up to 65,535 characters. Each character is stored as the corresponding Unicode value. String variables are suitable for word processing within programs and for temporary storage of any non-printable character up to a maximum length of 64 Kbytes. The memory required for storing string variables depends on the number of characters in the variable. The type-declaration character is "$".
文字列変数には、最大 65,535 文字の文字列を収めることができます。格納後の文字列は、対応する Unicode 値に変換されます。文字列変数の用途としては、プログラム内での文字列処理を行う場合や、最大 64 キロバイトの非印刷文字を一時的に格納する場合などに適しています。文字列変数が消費するメモリ量は、格納する文字列の長さに依存します。型宣言用の記号は「$」です。(par_id3151393.59)

Dim Variable$
Dim 変数名$(par_id3166467.60)

Dim Variable As String
Dim 変数名 As String(par_id3153027.61)

Boolean Variables
ブール型変数(hd_id3150534.62)

Boolean variables store only one of two values: TRUE or FALSE. A number 0 evaluates to FALSE, every other value evaluates to TRUE.
ブール型変数には、TRUE (真) および FALSE (偽) という 2 つの値の一方のみを収めることができます。数値 0 は FALSE に評価され、その他すべての値は TRUE に評価されます。(par_id3145632.63)

Dim Variable As Boolean
Dim 変数名 As Boolean(par_id3147615.64)

Date Variables
日付変数(hd_id3149722.65)

Date variables can only contain dates and time values stored in an internal format. Values assigned to Date variables with Dateserial, Datevalue, Timeserial or Timevalue are automatically converted to the internal format. Date-variables are converted to normal numbers by using the Day, Month, Year or the Hour, Minute, Second function. The internal format enables a comparison of date/time values by calculating the difference between two numbers. These variables can only be declared with the key word Date.
日付変数には、日付と時刻を示す値を内部形式で収めることができます。 日付変数へ値を代入する際に、DateserialDatevalueTimeserialTimevalue を使用すると、自動的に内部形式へ変換されます。 日付変数の値を通常の数値に変換するには、DayMonthYear の各関数および HourMinuteSecond の各関数を使用できます。 時刻や日付は、内部形式に変換することにより、時刻間の比較計算など行えます。 日付変数は、キーワード Date でのみ宣言できます。(par_id3159116.66)

Dim Variable As Date
Dim 変数名 As Date(par_id3150462.67)

Initial Variable Values
変数の初期値(hd_id3148732.68)

As soon as the variable has been declared, it is automatically set to the "Null" value. Note the following conventions:
宣言された変数には、初期値として「NULL」値が自動的に設定されます。変数型ごとに下記の規約が適用されるので、注意してください。(par_id3154549.69)

Numeric variables are automatically assigned the value "0" as soon as they are declared.
数値 変数には、宣言後に「0」が自動的に代入されます。(par_id3143222.70)

Date variables are assigned the value 0 internally; equivalent to converting the value to "0" with the Day, Month, Year or the Hour, Minute, Second function.
日付変数 には、初期値として「0」が代入されます。これは、DayMonthYear の各関数および HourMinuteSecond の各関数を使用して値を「0」に変換した場合と同じです。(par_id3150693.71)

String variables are assigned an empty-string ("") when they are declared.
文字列変数 には、宣言後に空の文字列 ("") が代入されます。(par_id3154807.72)

Arrays
配列(hd_id3153936.83)

Office Basic knows one- or multi-dimensional arrays, defined by a specified variable type. Arrays are suitable for editing lists and tables in programs. Individual elements of an array can be addressed through a numeric index.
Office Basic では 1 次元および多次元配列を使用することができ、変数宣言をする際に変数型を指定します。配列は、プログラム中でリストやテーブルを操作する場合に適しています。配列の各要素の指定は、数値によるインデックスで行います。(par_id3148736.84)

Arrays must be declared with the Dim statement. There are several ways to define the index range of an array:
配列の宣言には、Dim ステートメントを使う必要があります。配列の長さ (インデックス) の指定は、何通りかの方法があります。(par_id3149546.85)

DIM text$(20)
DIM text$(20)(par_id3150143.86)

21 elements numbered from 0 to 20
この場合は、0から 20 まで計 21 個の要素が確保されます。(par_id3154567.136)

DIM text$(5,4)
DIM text$(5,4)(par_id3145596.125)

30 elements (a matrix of 6 x 5 elements)
この場合は、計 30 個の要素が確保されます (6 x 5 要素の行列として)。(par_id3154397.137)

DIM text$(5 to 25)
DIM text$(5 to 25)(par_id3149185.87)

21 elements numbered from 5 to 25
この場合は、5 から 25 まで計 21 個の要素が確保されます。(par_id3149690.138)

DIM text$(-15 to 5)
DIM text$(-15 to 5)(par_id3155950.88)

21 elements (including 0), numbered from -15 to 5
この場合は、-15 から 5 まで (0 も含めた) 計 21 個の要素が確保されます。(par_id3153113.89)


The index range can include positive as well as negative numbers.
インデックスの範囲には、正の値だけでなく負の値も使用できます。 (par_id3153005.90)

Constants
定数(hd_id3154507.91)

Constants have a fixed value. They are only defined once in the program and cannot be redefined later:
定数値には、固定された一定の値を割り当てます。これらはプログラム中で一度だけ定義することができ、その後再定義することはできません。(par_id3156357.92)

CONST ConstName=Expression
CONST ConstName=Expression(par_id3153203.93)


< Prev / Next >